package com.ssm.controller;

import com.ssm.domain.Permission;
import com.ssm.domain.Role;
import com.ssm.domain.UserInfo;
import com.ssm.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * @Author: my_code
 * @Description: 角色管理
 * @Date: Create in 17:09 2020/8/26
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    //查询所有角色
    @RequestMapping("/findAll.do")
    public ModelAndView findAll() throws Exception {
        ModelAndView mv = new ModelAndView();
        List<Role> roleList = roleService.findAll();
        mv.addObject("roleList", roleList);
        mv.setViewName("role-list");
        return mv;
    }

    @RequestMapping("/findById.do")
    public ModelAndView findById(String id) {
        ModelAndView mv = new ModelAndView();
        Map<String,List> roleInfo = roleService.findRoleInfoById(id);
        mv.addObject("users", roleInfo.get("users"));
        mv.addObject("permissions", roleInfo.get("permissions"));
        mv.setViewName("role-show");
        return mv;
    }

    //新建角色保存
    @RequestMapping("/save.do")
    public String save(Role role) throws Exception {
        roleService.save(role);
        return "redirect:findAll.do";
    }


    /**
     * @param : [userIds, roleId]
     * @return : java.lang.String
     * @Description : 实现角色添加用户操作
     */
    @RequestMapping("/addUserToRole.do")
    public String addUserToRole(@RequestParam(name = "ids") String[] userIds, @RequestParam(name = "roleId") String roleId) throws Exception {
        roleService.addUserToRole(userIds, roleId);
        return "forward:findUserByRoleId.do?id=" + roleId;
    }

    /**
     * @param : [roleId]
     * @return : org.springframework.web.servlet.ModelAndView
     * @Description : 这是角色添加用户的准备工作，先拿到这个角色目前的的详细信息，
     * 根据拿到的信息去数据库查询符合添加要求的用户，并反馈到页面上。
     */
    @RequestMapping("/findUserByRoleId.do")
    public ModelAndView findUserByRoleId(@RequestParam(name = "id") String roleId) throws Exception {
        ModelAndView mv = new ModelAndView();
        Role role = roleService.findByRoleId(roleId);
        List<UserInfo> otherUsers = roleService.findOtherUsers(roleId);
        mv.addObject("role", role);
        mv.addObject("userList", otherUsers);
        mv.setViewName("role-user-add");
        return mv;
    }


    /**
     * @param : [id]
     * @return : java.lang.String
     * @Description : 通过roleId删除用户，（并删除与角色关联的表的数据，即一并删除user_role,permission_role中的相关数据）
     * 最后返回 role-list.jsp
     */
    @RequestMapping("/deleteById.do")
    public String deleteById(String id) {
        roleService.deleteById(id);
        return "redirect:findAll.do";
    }
}
